home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_strop.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  5.5 KB  |  134 lines

  1. import warnings
  2. warnings.filterwarnings("ignore", "", DeprecationWarning, __name__)
  3. warnings.filterwarnings("ignore", "", DeprecationWarning, "unittest")
  4. import strop
  5. import test_support
  6. import unittest
  7.  
  8.  
  9. class StropFunctionTestCase(unittest.TestCase):
  10.  
  11.     def test_atoi(self):
  12.         self.assert_(strop.atoi(" 1 ") == 1)
  13.         self.assertRaises(ValueError, strop.atoi, " 1x")
  14.         self.assertRaises(ValueError, strop.atoi, " x1 ")
  15.  
  16.     def test_atol(self):
  17.         self.assert_(strop.atol(" 1 ") == 1L)
  18.         self.assertRaises(ValueError, strop.atol, " 1x")
  19.         self.assertRaises(ValueError, strop.atol, " x1 ")
  20.  
  21.     def test_atof(self):
  22.         self.assert_(strop.atof(" 1 ") == 1.0)
  23.         self.assertRaises(ValueError, strop.atof, " 1x")
  24.         self.assertRaises(ValueError, strop.atof, " x1 ")
  25.  
  26.     def test_capitalize(self):
  27.         self.assert_(strop.capitalize(" hello ") == " hello ")
  28.         self.assert_(strop.capitalize("hello ") == "Hello ")
  29.  
  30.     def test_find(self):
  31.         self.assert_(strop.find("abcdefghiabc", "abc") == 0)
  32.         self.assert_(strop.find("abcdefghiabc", "abc", 1) == 9)
  33.         self.assert_(strop.find("abcdefghiabc", "def", 4) == -1)
  34.  
  35.     def test_rfind(self):
  36.         self.assert_(strop.rfind("abcdefghiabc", "abc") == 9)
  37.  
  38.     def test_lower(self):
  39.         self.assert_(strop.lower("HeLLo") == "hello")
  40.  
  41.     def test_upper(self):
  42.         self.assert_(strop.upper("HeLLo") == "HELLO")
  43.  
  44.     def test_swapcase(self):
  45.         self.assert_(strop.swapcase("HeLLo cOmpUteRs") == "hEllO CoMPuTErS")
  46.  
  47.     def test_strip(self):
  48.         self.assert_(strop.strip(" \t\n hello \t\n ") == "hello")
  49.  
  50.     def test_lstrip(self):
  51.         self.assert_(strop.lstrip(" \t\n hello \t\n ") == "hello \t\n ")
  52.  
  53.     def test_rstrip(self):
  54.         self.assert_(strop.rstrip(" \t\n hello \t\n ") == " \t\n hello")
  55.  
  56.     def test_replace(self):
  57.         replace = strop.replace
  58.         self.assert_(replace("one!two!three!", '!', '@', 1)
  59.                      == "one@two!three!")
  60.         self.assert_(replace("one!two!three!", '!', '@', 2)
  61.                      == "one@two@three!")
  62.         self.assert_(replace("one!two!three!", '!', '@', 3)
  63.                      == "one@two@three@")
  64.         self.assert_(replace("one!two!three!", '!', '@', 4)
  65.                      == "one@two@three@")
  66.  
  67.         # CAUTION: a replace count of 0 means infinity only to strop,
  68.         # not to the string .replace() method or to the
  69.         # string.replace() function.
  70.  
  71.         self.assert_(replace("one!two!three!", '!', '@', 0)
  72.                      == "one@two@three@")
  73.         self.assert_(replace("one!two!three!", '!', '@')
  74.                      == "one@two@three@")
  75.         self.assert_(replace("one!two!three!", 'x', '@')
  76.                      == "one!two!three!")
  77.         self.assert_(replace("one!two!three!", 'x', '@', 2)
  78.                      == "one!two!three!")
  79.  
  80.     def test_split(self):
  81.         split = strop.split
  82.         self.assert_(split("this is the split function")
  83.                      == ['this', 'is', 'the', 'split', 'function'])
  84.         self.assert_(split("a|b|c|d", '|') == ['a', 'b', 'c', 'd'])
  85.         self.assert_(split("a|b|c|d", '|', 2) == ['a', 'b', 'c|d'])
  86.         self.assert_(split("a b c d", None, 1) == ['a', 'b c d'])
  87.         self.assert_(split("a b c d", None, 2) == ['a', 'b', 'c d'])
  88.         self.assert_(split("a b c d", None, 3) == ['a', 'b', 'c', 'd'])
  89.         self.assert_(split("a b c d", None, 4) == ['a', 'b', 'c', 'd'])
  90.         self.assert_(split("a b c d", None, 0) == ['a', 'b', 'c', 'd'])
  91.         self.assert_(split("a  b  c  d", None, 2) ==  ['a', 'b', 'c  d'])
  92.  
  93.     def test_join(self):
  94.         self.assert_(strop.join(['a', 'b', 'c', 'd']) == 'a b c d')
  95.         self.assert_(strop.join(('a', 'b', 'c', 'd'), '') == 'abcd')
  96.         self.assert_(strop.join(Sequence()) == 'w x y z')
  97.  
  98.         # try a few long ones
  99.         self.assert_(strop.join(['x' * 100] * 100, ':')
  100.                      == (('x' * 100) + ":") * 99 + "x" * 100)
  101.         self.assert_(strop.join(('x' * 100,) * 100, ':')
  102.                      == (('x' * 100) + ":") * 99 + "x" * 100)
  103.  
  104.     def test_maketrans(self):
  105.         self.assert_(strop.maketrans("abc", "xyz") == transtable)
  106.         self.assertRaises(ValueError, strop.maketrans, "abc", "xyzq")
  107.  
  108.     def test_translate(self):
  109.         self.assert_(strop.translate("xyzabcdef", transtable, "def")
  110.                      == "xyzxyz")
  111.  
  112.     def test_data_attributes(self):
  113.         strop.lowercase
  114.         strop.uppercase
  115.         strop.whitespace
  116.  
  117.  
  118. transtable = '\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377'
  119.  
  120.  
  121. # join() now works with any sequence type.
  122. class Sequence:
  123.     def __init__(self): self.seq = 'wxyz'
  124.     def __len__(self): return len(self.seq)
  125.     def __getitem__(self, i): return self.seq[i]
  126.  
  127.  
  128. def test_main():
  129.     test_support.run_unittest(StropFunctionTestCase)
  130.  
  131.  
  132. if __name__ == "__main__":
  133.     test_main()
  134.